#include "qpenproperty.h"

#include "qintproperty.h"
#include "qenumproperty.h"
#include "qcolorproperty.h"

#include "../qcommonstruct.h"
#include "../qpropertyfactory.h"

#include <QFontDatabase>
#include <QApplication>
#include <QPen>
#include <QImage>
#include <QPainter>

QPenProperty::QPenProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    set_property("type","Pen");

    m_pen_style=new QEnumProperty(this);
    m_pen_style->set_attribute("show_name",tr("Style"));
    m_pen_style->set_property("name","pen_style");
    tagComboItem item;
    ComboItems  items;

    item.m_text="Solid Line";
    item.m_value=Qt::SolidLine;
    items.append(item);

    item.m_text="Dash Line";
    item.m_value=Qt::DashLine;
    items.append(item);

    item.m_text="Dot Line";
    item.m_value=Qt::DotLine;
    items.append(item);

    item.m_text="Dash Dot Line";
    item.m_value=Qt::DashDotLine;
    items.append(item);

    item.m_text="Dash Dot Dot Line";
    item.m_value=Qt::DashDotDotLine;
    items.append(item);

    m_pen_style->set_attribute("items",QVariant::fromValue<ComboItems>(items));


    m_pen_width=new QIntProperty(this);
    m_pen_width->set_attribute("show_name",tr("Width"));
    m_pen_width->set_property("name","pen_width");

    m_color=new QColorProperty(this);
    m_color->set_attribute("show_name",tr("Color"));
    m_color->set_property("name","pen_color");

    m_cap_style=new QEnumProperty(this);
    m_cap_style->set_attribute("show_name",tr("Cap Style"));
    m_cap_style->set_property("name","pen_cap_style");

    items.clear();

    item.m_text="Square Cap";
    item.m_value=Qt::SquareCap;
    items.append(item);

    item.m_text="Flat Cap";
    item.m_value=Qt::FlatCap;
    items.append(item);

    item.m_text="Round Cap";
    item.m_value=Qt::RoundCap;
    items.append(item);

    m_cap_style->set_attribute("items",QVariant::fromValue<ComboItems>(items));

    m_join_style=new QEnumProperty(this);
    m_join_style->set_attribute("show_name",tr("Join Style"));
    m_join_style->set_property("name","pen_join_style");

    items.clear();

    item.m_text="Bevel Join";
    item.m_value=Qt::BevelJoin;
    items.append(item);

    item.m_text="Miter Join";
    item.m_value=Qt::MiterJoin;
    items.append(item);

    item.m_text="Round Join";
    item.m_value=Qt::RoundJoin;
    items.append(item);

    m_join_style->set_attribute("items",QVariant::fromValue<ComboItems>(items));



    tagPenInfo info;
    info.m_pen_cap_style=Qt::FlatCap;
    info.m_pen_join_style=Qt::MiterJoin;
    info.m_width=1;
    info.m_pen_style=Qt::SolidLine;
    QLinearGradient g(0,0,0,1);
    g.setColorAt(0,QColor(0,0,0));
    g.setColorAt(1,QColor(0,0,0));
    info.m_color=g;

    m_value.setValue<tagPenInfo>(info);
}

void QPenProperty::set_value(const QVariant &value)
{
    if(!value.isValid())
    {
        return;
    }

    disconnect_children();

    tagPenInfo pen=value.value<tagPenInfo>();
    m_pen_style->set_value(pen.m_pen_style);
    m_join_style->set_value(pen.m_pen_join_style);
    m_cap_style->set_value(pen.m_pen_cap_style);
    m_pen_width->set_value(pen.m_width);
    m_color->set_value(QVariant::fromValue<QGradient>(pen.m_color));
    QAbstractProperty::set_value(value);

    connect_children();
}

QString QPenProperty::get_value_text()
{
    return m_pen_style->get_value_text();
}

QIcon QPenProperty::get_value_icon()
{
    tagPenInfo info=get_value().value<tagPenInfo>();
    QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
    img.fill(0);
    QPainter p(&img);
    p.setRenderHint(QPainter::Antialiasing, true);
    QPen pen=p.pen();

    pen.setWidth(1);
    pen.setStyle(info.m_pen_style);
    pen.setJoinStyle(info.m_pen_join_style);
    pen.setCapStyle(info.m_pen_cap_style);
    pen.setBrush(QBrush(info.m_color));

    p.setPen(pen);

    p.drawLine(0,0,16,16);
    p.drawLine(0,16,16,0);

    return QPixmap::fromImage(img);
}

void QPenProperty::child_value_changed(const QVariant &, const QVariant &)
{
    tagPenInfo pen=get_value().value<tagPenInfo>();
    pen.m_color=m_color->get_value().value<QGradient>();
    pen.m_pen_style=(Qt::PenStyle)m_pen_style->get_value().toInt();
    pen.m_pen_cap_style=(Qt::PenCapStyle)m_cap_style->get_value().toInt();
    pen.m_pen_join_style=(Qt::PenJoinStyle)m_join_style->get_value().toInt();
    pen.m_width=m_pen_width->get_value().toInt();
    QAbstractProperty::set_value(QVariant::fromValue<tagPenInfo>(pen));
}

void QPenProperty::make_value()
{
    tagPenInfo pen=get_value().value<tagPenInfo>();
    pen.m_color=m_color->get_value().value<QGradient>();
    pen.m_pen_style=(Qt::PenStyle)m_pen_style->get_value().toInt();
    pen.m_pen_cap_style=(Qt::PenCapStyle)m_cap_style->get_value().toInt();
    pen.m_pen_join_style=(Qt::PenJoinStyle)m_join_style->get_value().toInt();
    pen.m_width=m_pen_width->get_value().toInt();
    m_value.setValue<tagPenInfo>(pen);
}

void QPenProperty::write_value()
{

}

QString QPenProperty::get_property_name()
{
    return "Pen";
}

bool QPenProperty::cmp(const QVariant &value)
{
    tagPenInfo info1=get_value().value<tagPenInfo>();
    tagPenInfo info2=value.value<tagPenInfo>();

    return info1==info2;
}

qRegisterProperty(QPenProperty)
